package com.time.admin.application.controller;

import com.google.common.base.Preconditions;
import com.time.admin.application.converter.SettingDTOConverter;
import com.time.admin.application.dto.SettingDTO;
import com.time.admin.domain.entity.SettingBO;
import com.time.admin.domain.service.AdminSettingDomainService;
import com.time.auth.entity.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 系统设置管理控制层类
 *
 * @author: HuangQi
 * @date: Created in 11:40 2025/11/11
 * @description: 系统设置管理控制层
 */
@Slf4j
@RestController
@RequestMapping("/admin/setting")
public class SettingController {

    @Autowired
    private AdminSettingDomainService adminSettingDomainService;

    /**
     * 更新配置
     *
     * @param postDTO 更新设置的信息
     * @return 更新结果
     */
    @PostMapping("update/{name}")
    public Result<Boolean> update(@RequestBody SettingDTO settingDTO,
                                  @PathVariable("name") String name) {
        try {
            if (log.isInfoEnabled()) {
                log.info("Admin.AdminSettingDomainService.update.dto:{}", settingDTO);
            }
            // 参数校验
            Preconditions.checkNotNull(name, "配置的名称不能为空");


            // 调用领域层
            SettingBO settingBO = SettingDTOConverter.INSTANCE.convertDTOToBO(settingDTO);
            Boolean updateResult = adminSettingDomainService.update(settingBO, name);

            // 返回结果
            return Result.ok(updateResult);
        } catch (Exception e) {
            // 异常处理
            log.error("Admin.SettingController.update.error:{}", e.getMessage(), e);
            return Result.fail("管理模块更新配置信息失败");
        }
    }

    /**
     * 获取配置信息
     *
     * @param id 配置的名称
     * @return 配置的详细信息
     */
    @PostMapping("query/{name}")
    public Result<SettingDTO> querySetting(@PathVariable("name") String name) {
        try {
            if (log.isInfoEnabled()) {
                log.info("Admin.AdminSettingDomainService.querySetting.name: {}", name);
            }
            // 参数校验
            Preconditions.checkNotNull(name, "配置的名称不能为空");

            // 调用领域层
            SettingBO settingBO = adminSettingDomainService.querySetting(name);

            // 返回结果
            SettingDTO settingDTO = SettingDTOConverter.INSTANCE.convertBOToDTO(settingBO);
            return Result.ok(settingDTO);
        } catch (Exception e) {
            // 异常处理
            log.error("Admin.SettingController.querySetting.error:{}", e.getMessage(), e);
            return Result.fail("管理模块分页获取配置的详细信息失败");
        }
    }
}
